﻿Imports System.Data.SqlClient

Public Class clsClaves
    Private mintIdClave As Integer
    Private mstrClave As String
    Private mstrDescripcion As String
    Private mstrClaveDepto As String
    Private mstrDepartamento As String
    Private mdblPrecio As Double
    Private mintTipo As Integer
    Private mintEstatus As Integer
    Private mstrError As String
    Private mstrNombre As String

    Public Property IdClave() As Integer
        Get
            Return mintIdClave
        End Get
        Set(ByVal value As Integer)
            mintIdClave = value
        End Set
    End Property
    Public Property Clave() As String
        Get
            Return mstrClave
        End Get
        Set(ByVal value As String)
            mstrClave = value
        End Set
    End Property
    Public Property Errores() As String
        Get
            Return mstrError
        End Get
        Set(ByVal value As String)
            mstrError = value
        End Set
    End Property
    Public Property Descripcion() As String
        Get
            Return mstrDescripcion
        End Get
        Set(ByVal value As String)
            mstrDescripcion = value
        End Set
    End Property
    Public Property ClaveDepto() As String
        Get
            Return mstrClaveDepto
        End Get
        Set(ByVal value As String)
            mstrClaveDepto = value
        End Set
    End Property
    Public Property Departamento() As String
        Get
            Return mstrDepartamento
        End Get
        Set(ByVal value As String)
            mstrDepartamento = value
        End Set
    End Property
    Public Property Precio() As Double
        Get
            Return mdblPrecio
        End Get
        Set(value As Double)
            mdblPrecio = value
        End Set
    End Property
   
    Public Property Tipo() As Integer
        Get
            Return mintTipo
        End Get
        Set(ByVal value As Integer)
            mintTipo = value
        End Set
    End Property
    Public Property Estatus() As Integer
        Get
            Return mintEstatus
        End Get
        Set(ByVal value As Integer)
            mintEstatus = value
        End Set
    End Property
    Public Sub New()
        mintIdClave = 0
        mstrClave = ""
        mstrDescripcion = ""
        mstrClaveDepto = ""
        mstrDepartamento = ""
        mdblPrecio = 0
        mintTipo = 0
        mintEstatus = 0
        mstrError = ""
    End Sub
    Public Sub Buscar()
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT * From CLAVES_DESTAJO " & _
        " WHERE IdClave=" & IdClave

        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            mstrClave = rdBuscar("Clave")
            mstrDescripcion = rdBuscar("Descripción")
            mstrClaveDepto = rdBuscar("ClaveDepto")
            mstrDepartamento = rdBuscar("Departamento")
            mdblPrecio = rdBuscar("Precio")
            mintTipo = rdBuscar("Tipo")
            mintEstatus = rdBuscar("Estatus")
        Else
            mstrClave = ""
            mstrDescripcion = ""
            mstrClaveDepto = ""
            mstrDepartamento = ""
            mdblPrecio = 0
            mintTipo = 0
            mintEstatus = 0
        End If

        rdBuscar.Close()
        cnConn.Close()


    End Sub
    Public Function Insertar() As Integer
        Dim sql As String = "Insert Into CLAVES_DESTAJO (" & _
                            "Clave," & _
                            "Descripción," & _
                            "ClaveDepto," & _
                            "Departamento," & _
                            "Precio," & _
                            "Tipo," & _
                            "Estatus)" & _
                            "VALUES (" & _
                            "@Clave," & _
                            "@Descripcion," & _
                            "@ClaveDepto," & _
                            "@Departamento," & _
                            "@Precio," & _
                            "@Tipo," & _
                            "@Estatus)"


        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("Clave", mstrClave)
        Command.Parameters.AddWithValue("Descripcion", mstrDescripcion)
        Command.Parameters.AddWithValue("ClaveDepto", mstrClaveDepto)
        Command.Parameters.AddWithValue("Departamento", mstrDepartamento)
        Command.Parameters.AddWithValue("Precio", mdblPrecio)
        Command.Parameters.AddWithValue("Tipo", mintTipo)
        Command.Parameters.AddWithValue("Estatus", mintEstatus)

        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
            If RowsAffected > 0 Then
                cnConn.Close()
                mstrError = ""
                Return 0
            Else
                If cnConn.State = ConnectionState.Open Then
                    cnConn.Close()
                End If
                mstrError = ""
                Return 1
            End If
        Catch ex As Exception
            mstrError = ex.Message
            Return 2
        End Try


    End Function
    Public Function Actualizar() As Integer

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        strSqlQry = "Update CLAVES_DESTAJO Set " & _
                            "DESCRIPCION=@DESCRIPCION," & _
                            "CLAVEDEPTO=CLAVEDEPTO," & _
                            "DEPARTAMENTO=@DEPARTAMENTO," & _
                            "PRECIO=@PRECIO," & _
                            "TIPO=@TIPO," & _
                            "ESTATUS=@ESTATUS " & _
                            " where CLAVE=@CLAVE"


        Dim Command As New SqlCommand(strSqlQry, cnConn)
        Command.Parameters.AddWithValue("Clave", mstrClave)
        Command.Parameters.AddWithValue("Descripcion", mstrDescripcion)
        Command.Parameters.AddWithValue("ClaveDepto", mstrClaveDepto)
        Command.Parameters.AddWithValue("Departamento", mstrDepartamento)
        Command.Parameters.AddWithValue("Precio", mdblPrecio)
        Command.Parameters.AddWithValue("Tipo", mintTipo)
        Command.Parameters.AddWithValue("Estatus", mintEstatus)

        cnConn.Open()

        Try
            Dim RowsAffected As Integer = Command.ExecuteNonQuery

            If RowsAffected > 0 Then
                cnConn.Close()
                mstrError = ""
                Return 0
            Else

                If cnConn.State = ConnectionState.Open Then
                    cnConn.Close()
                End If
                mstrError = ""
                Return 1
            End If
        Catch ex As Exception
            mstrError = ex.Message
            Return 2
        End Try




    End Function
    Public Sub GUARDA_ACTIVIDAD(ByVal CLAVE As String, ByVal NOMBRE As String, ByVal DESCRIPCION As String, ByVal COD_DEPTO As String, ByVal DEPARTAMENTO As String, ByVal TIEMPO As Double, ByVal CAPACIDAD As Double, ByVal INDIVIDUAL As Double, ByVal TIPO As Integer)
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = CitraConnection

        Dim Command As New SqlCommand("sp_ACTIVIDAD_INSERTAR", cnConn)
        Command.CommandType = CommandType.StoredProcedure

        Command.Parameters.Add(New SqlParameter("@CLAVE", SqlDbType.VarChar, 50))
        Command.Parameters("@CLAVE").Value = CLAVE
        Command.Parameters.Add(New SqlParameter("@NOMBRE", SqlDbType.VarChar, 100))
        Command.Parameters("@NOMBRE").Value = NOMBRE
        Command.Parameters.Add(New SqlParameter("@DESCRIPCION", SqlDbType.VarChar, 1000))
        Command.Parameters("@DESCRIPCION").Value = DESCRIPCION
        Command.Parameters.Add(New SqlParameter("@CLAVEDEPTO", SqlDbType.VarChar, 50))
        Command.Parameters("@CLAVEDEPTO").Value = COD_DEPTO
        Command.Parameters.Add(New SqlParameter("@DEPARTAMENTO", SqlDbType.VarChar, 200))
        Command.Parameters("@DEPARTAMENTO").Value = DEPARTAMENTO
        Command.Parameters.Add(New SqlParameter("@TIEMPO", SqlDbType.Decimal))
        Command.Parameters("@TIEMPO").Value = TIEMPO
        Command.Parameters.Add(New SqlParameter("@CAPACIDAD", SqlDbType.Decimal))
        Command.Parameters("@CAPACIDAD").Value = CAPACIDAD
        Command.Parameters.Add(New SqlParameter("@INDIVIDUAL", SqlDbType.Decimal))
        Command.Parameters("@INDIVIDUAL").Value = INDIVIDUAL
        Command.Parameters.Add(New SqlParameter("@TIPO", SqlDbType.Int))
        Command.Parameters("@TIPO").Value = TIPO

        Dim Lector As SqlDataReader
        Try
            cnConn.Open()
            Lector = Command.ExecuteReader
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try


    End Sub
End Class
